package com.cnhis.cloudhealth.clinical.hosdoctor.doctororderdelete.bo;

import com.cnhis.cloudhealth.clinical.hosdoctor.doctororderdelete.dao.DoctororderdeleteDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DoctororderdeleteBo {
	@Resource
	private DoctororderdeleteDao doctororderdeleteDao;
	
	@Autowired
	private GetNewId getNewId;

	private List<Map<String,String>> changeToString(List<?> list){
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		for(Object o:list){
			if(o instanceof Map){
				Map<String,Object> pmap = (Map<String,Object>)o;
				Map<String,String> smap = new HashMap<String,String>();
				Iterator<Map.Entry<String, Object>> it = pmap.entrySet().iterator();
				while (it.hasNext()) {
					Map.Entry<String, Object> entry = it.next();
					String str = String.valueOf(entry.getValue());
					smap.put(entry.getKey(),str);
				}
				resultList.add(smap);
			}
		}
		return resultList;
	}
	private ModelVo packModelVo(String nid,Map map){
		ModelVo mo = new ModelVo();
		List<?> list = null;
		try {
			list = doctororderdeleteDao.queryForList(nid,map);
			list = changeToString(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list!=null&&list.size()>0) {
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(list.size());
			mo.setRows(list);
		}
		return mo;
	}

	public ModelVo delclinicAdvice(Map map) throws Exception {
		String lxml = MapUtils.getString(map,"lxml","");

		if(StringUtils.isBlank(lxml)){
			return new ModelVo(StaticKeys.OH_FALSE, "参数为空，请检查参数！");
		}

		//将Json数据装换为list
		List<Map<String,Object>> ls = JsonUtil.getList(lxml);

		//根据医嘱ID获取医嘱详情
		List<Map<String,Object>> tmpVafdf = getAdviceDetailByID(ls);

		//过滤VAF10 >= 3的医嘱信息
		tmpVafdf = getGtThree(tmpVafdf);

		if(tmpVafdf.size()==0){
			return new ModelVo(StaticKeys.OH_FALSE, "医嘱都已执行，没有可以删除的医嘱！");
		}
		// 医嘱单ID去重
        List TmpCBMall = getTmpCBMall(tmpVafdf);
        // 获取TmpCBMall对应的医嘱ID但却不在TmpVafdf中
        List TmpVAFdno = getTmpVAFdno(TmpCBMall, tmpVafdf);
        
        // 获取TmpCBMall对应的医嘱ID但却不在TmpVAFdno中
        List tmpCBMd = getTmpCBMdByTmpCBMall(TmpCBMall, TmpVAFdno);
        
        // 获取医嘱的申请单信息
        List tmpVBQd = getTmpVBQdByTmpVafdf(tmpVafdf, TmpVAFdno);
		String bce03 = map.get("bce03").toString();

		String aDate = DateUtils.getSystemDate();
		//记录医嘱删除操作日志
		List<Map<String,Object>> inVBG1s = getInVBG1s(tmpVafdf,aDate,bce03);
		insertDelVbg(inVBG1s);

		delMzCBM2(tmpCBMd);
		delMzVBQ2(tmpVBQd);
		delMzVAF2(tmpVafdf);
		delMzVAO2(tmpCBMd);

		//医嘱删除医嘱单删除
		List<Map<String,Object>> inVbgDels = getInVbgDels(tmpVafdf,aDate,bce03);
		if(inVbgDels!=null&&inVbgDels.size()>0) {
			insertVbgDel(inVbgDels);
		}

		//删除病人医嘱路径
		delVCR1ByTmpVafdf(tmpVafdf);

		//更新病人医嘱提醒状态
		updateVBZ1ByTmpVafdf(tmpVafdf);



		return new ModelVo(StaticKeys.OH_SUCCESS, "删除成功");
	}
	 private List getTmpVBQdByTmpVafdf(List tmpVafdf, List tmpVAFdno)
	    {
	        List ls = new ArrayList<>();
	        
	        int tv = tmpVAFdno.size();
	        int tvf = tmpVafdf.size();
	        
	        Map map = null;
	        String vaf51 = null;
	        
	        Set sets = new HashSet<>();
	        
	        // 获取tmpVAFdno的CBM01字段；
	        Set set = new HashSet<>();
	        for (int i = 0; i < tv; i++)
	        {
	            map = (Map)tmpVAFdno.get(i);
	            if (map.containsKey("vaf51"))
	            {
	                vaf51 = map.get("vaf51").toString();
	                if (vaf51 != null)
	                {
	                    set.add(vaf51);
	                }
	            }
	        }
	        
	        // 获取结果集
	        for (int i = 0; i < tvf; i++)
	        {
	            map = (Map)tmpVafdf.get(i);
	            if (map.containsKey("vaf51"))
	            {
	                vaf51 = map.get("vaf51").toString();
	                if (vaf51 != null)
	                {
	                    if (!set.contains(vaf51))
	                    {
	                        sets.add(vaf51);
	                    }
	                }
	            }
	            
	        }
	        
	        for (Iterator it = sets.iterator(); it.hasNext();)
	        {
	            vaf51 = (String)it.next();
	            map = new HashMap<>();
	            map.put("vbq01", vaf51);
	            ls.add(map);
	        }
	        
	        return ls;
	    }
	    
	    private List getTmpCBMdByTmpCBMall(List tmpCBMall, List tmpVAFdno)
	    {
	        
	        List ls = new ArrayList<>();
	        
	        int tv = tmpVAFdno.size();
	        int tc = tmpCBMall.size();
	        
	        Map map = null;
	        Long cbm01 = null;
	        
	        // 获取tmpVAFdno的CBM01字段；
	        Set set = new HashSet<>();
	        for (int i = 0; i < tv; i++)
	        {
	            map = (Map)tmpVAFdno.get(i);
	            if (map.containsKey("cbm01"))
	            {
	                cbm01 = Long.valueOf(map.get("cbm01").toString());
	                if (cbm01 != null)
	                {
	                    set.add(cbm01);
	                }
	            }
	        }
	        
	        // 获取结果集
	        for (int i = 0; i < tc; i++)
	        {
	            map = (Map)tmpCBMall.get(i);
	            if (map.containsKey("cbm01"))
	            {
	                cbm01 = Long.valueOf(map.get("cbm01").toString());
	                if (cbm01 != null)
	                {
	                    if (!set.contains(cbm01))
	                    {
	                        ls.add(map);
	                    }
	                }
	            }
	            
	        }
	        return ls;
	    }
	    
	    /**
	     * 获取TmpCBMall对应的医嘱ID但却不在TmpVafdf中
	     * 
	     * @param tmpCBMall
	     * @param tmpVafdf
	     * @return
	     * @throws Exception
	     */
	    private List getTmpVAFdno(List tmpCBMall, List tmpVafdf)
	        throws Exception
	    {
	        List vafs = getVafsByTmpCBMall(tmpCBMall);
	        
	        List ls = new ArrayList<>();
	        
	        int vf = vafs.size();
	        int tv = tmpVafdf.size();
	        
	        Map map = null;
	        Long vaf01 = null;
	        
	        // 获取tmpVafdf的vaf01字段；
	        Set set = new HashSet<>();
	        for (int i = 0; i < tv; i++)
	        {
	            map = (Map)tmpVafdf.get(i);
	            if (map.containsKey("vaf01"))
	            {
	                vaf01 = Long.valueOf(map.get("vaf01").toString());
	                if (vaf01 != null)
	                {
	                    set.add(vaf01);
	                }
	            }
	        }
	        
	        // 获取结果集
	        for (int i = 0; i < vf; i++)
	        {
	            map = (Map)vafs.get(i);
	            if (map.containsKey("vaf01"))
	            {
	                vaf01 = Long.valueOf(map.get("vaf01").toString());
	                if (vaf01 != null)
	                {
	                    if (!set.contains(vaf01))
	                    {
	                        ls.add(map);
	                    }
	                }
	            }
	            
	        }
	        
	        return ls;
	    }
	private void updateVBZ1ByTmpVafdf(List tmpVafdf) throws Exception {
		doctororderdeleteDao.updateVBZ1ByTmpVafdf(tmpVafdf);
	}

	private void delVCR1ByTmpVafdf(List tmpVafdf) throws Exception {
		doctororderdeleteDao.delVCR1ByTmpVafdf(tmpVafdf);
	}

	private void insertVbgDel(List inVBG1s) throws Exception {
		doctororderdeleteDao.insertVbgDel(inVBG1s);
	}

	private List getInVbgDels(List<Map<String,Object>> tmpCBMd, String aDate, String bce03) {
		int tc = tmpCBMd.size();
		List<Long> ls = getNewId.nextId(tc);
		
		Map<String,Object> map =null;
		for(int i=0;i<tc;i++){
			map = tmpCBMd.get(i);
			map.put("vbg01", ls.get(i));
//			map.put("vbg03",new Integer(0));
			map.put("bce03",bce03);
			map.put("vbg05",aDate);
			map.put("vbg06","医嘱删除医嘱单删除");
		}
		return tmpCBMd;
	}

	private void delMzVAO2(List tmpVafdf) throws Exception {
		if(tmpVafdf.size()>0){
			doctororderdeleteDao.delMzVAO2(tmpVafdf);
		}
	}

	private void delMzVAF2(List tmpVafdf) throws Exception {
		if(tmpVafdf.size()>0){
			doctororderdeleteDao.delMzVAF2(tmpVafdf);
		}
		
	}

	private void delMzVBQ2(List tmpVafdf) throws Exception {
		if(tmpVafdf.size()>0){
			doctororderdeleteDao.delMzVBQ2(tmpVafdf);
		}
	}

	private void delMzCBM2(List tmpVafdf) throws Exception {
		if(tmpVafdf.size()>0){
			doctororderdeleteDao.delMzCBM2(tmpVafdf);
		}
	}

	private void insertDelVbg(List<Map<String,Object>> inVBG1s) throws Exception {
		doctororderdeleteDao.insertDelVbg(inVBG1s);
	}

	private List<Map<String,Object>> getInVBG1s(List<Map<String,Object>> tmpVafdf, String aDate,String bce03) {
		int tv = tmpVafdf.size();
		List idS = getNewId.nextId(tv);
		Map<String,Object> map = null;
		for(int i=0;i<tv;i++){
			map = tmpVafdf.get(i);
			map.put("vbg01", idS.get(i));
			map.put("bce03",bce03);
			map.put("adate",aDate);
			map.put("vbg06","医嘱删除操作");
		}
		
		return tmpVafdf;
	}

	private List getVafsByTmpCBMall(List tmpCBMall) throws Exception {
		return doctororderdeleteDao.getVafsByTmpCBMall(tmpCBMall);
	}

	private List getTmpCBMall(List tmpVafdf) {
		List ls = new ArrayList();
		
		//去除医嘱单ID
		Set<Long> set = new HashSet<Long>();
		for(int i=0;i<tmpVafdf.size();i++){
			Map map = (Map) tmpVafdf.get(i);
			if(map.containsKey("cbm01")){
				Long cbm01 = Long.valueOf(map.get("cbm01").toString());
				if(cbm01!=null&&!cbm01.equals(0)){
					set.add(cbm01);
				}
			}
		}
		
		for(Long i:set){
			Map map = new HashMap<>();
			map.put("cbm01", i);
			ls.add(map);
		}
		
		
		
		return ls;
	}

	/**
	 * 过滤VAF10 >= 3的医嘱信息
	 * @param tmpVafdf
	 * @return
	 */
	private List getGtThree(List tmpVafdf) {
		if(tmpVafdf==null||tmpVafdf.size()==0){
			return new ArrayList();
		}
		
		List ls = new ArrayList();
		for(int i=0;i<tmpVafdf.size();i++){
			Map map = (Map) tmpVafdf.get(i);
			if(map.containsKey("vaf10")){
				Integer vaf10 = Integer.valueOf(map.get("vaf10").toString());
				vaf10 =vaf10==null?0:vaf10;
				if(vaf10<3){
					ls.add(map);
				}
			}
		}
		
		return ls;
	}

	private List getAdviceDetailByID(List ls) throws Exception {
		return doctororderdeleteDao.getAdviceDetailByID(ls);
	}
	private String getVAF59(Map map)throws Exception{
		return doctororderdeleteDao.getVAF59(map);
	}
	@SuppressWarnings("unchecked")
	public ModelVo deleteAdviceList(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List<Map<String,Object>> listAll =new ArrayList<Map<String,Object>>();
		try {
			map.put("VAF59", 0);
			if("2".equals(map.get("lVAF04").toString())){
				String vaf59 = getVAF59(map);
				map.put("VAF59", vaf59);
			}
			List<Map<String,Object>> list=doctororderdeleteDao.deleteAdviceList(map);
			listAll.addAll(list);
			 list=doctororderdeleteDao.deleteAdviceList1(map);
			listAll.addAll(list);
			 list=doctororderdeleteDao.deleteAdviceList2(map);
			listAll.addAll(list);
			 list=doctororderdeleteDao.deleteAdviceList3(map);
			listAll.addAll(list);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(listAll);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(listAll);
		return mo;
	}
	
	public ModelVo valuationSignDeleteAdvice(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List listAll =null;
		try {
			if("1".equals(map.get("acf01").toString())){
			  listAll = doctororderdeleteDao.valuationSignDeleteAdvice1(map);
			}else{
				listAll = doctororderdeleteDao.valuationSignDeleteAdvice(map);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(listAll);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(listAll);
		return mo;
	}

	public ModelVo selectOrderstatus(Map<String,Object> map){return packModelVo("Doctororderdelete.7747",map);}

}
